gh-152753: Fix memory leak in ipaddress properties by switching to cached_property#152790
gh-152753: Fix memory leak in ipaddress properties by switching to cached_property#152790bastitva0-blip wants to merge 8 commits into
Conversation
… to cached_property
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
f95478c to
a1fd3d8
Compare
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
All 49 checks have successfully passed! The PR is fully updated with the base branch and ready for review whenever a maintainer has some time. Thank you |
|
@pablogsal could you please take a look at this PR when you have a moment? Thanks! |
…FTAg1.rst Co-authored-by: Brij Kapadia <97006829+brijkapadia@users.noreply.github.com>
|
Hi @pablogsal @brijkapadia |
Fixes #152753
Description
Using
@functools.lru_cache()on instance methods creates a global cache that retains references toself. This keeps the instances alive indefinitely and prevents proper garbage collection, creating a memory leak.This PR resolves the issue by replacing the
lru_cachedecorators with@functools.cached_propertyon the affected lookup properties withinLib/ipaddress.py. This correctly ties the cache lifetime to the lifecycle of the instance itself.Changes
IPv4Address.is_private,IPv4Address.is_global,IPv4Network.is_global, andIPv6Address.is_private.'__dict__'to__slots__insideIPv4Addressso thatcached_propertyhas an active instance dictionary to store its cached values.'__dict__'to__slots__insideIPv6Addressfor the same reason, ensuring we preserve the memory efficiency benefits of slots for the remaining fixed attributes.